<template>
  <div ref="imageWrapper">
    <a-card :bordered="false">
      <!-- 查询区域 -->
      <div class="table-page-search-wrapper">
        <a-form-model ref="form" :model="queryParam" :rules="rules" layout="inline" @keyup.enter.native="searchQuery">
          <a-row :gutter="24">
            <!-- <a-col :md="4" :sm="6">
              <a-form-model-item label="计划类型">
                <j-dict-select-tag
                  style="width: 100%"
                  v-model="queryParam.planType"
                  placeholder="请选择"
                  dict-code="inconplantype"
                />
              </a-form-model-item>
            </a-col>-->
            <a-col :md="5" :sm="6">
              <a-form-model-item label="账套/核算单元" prop="accName">
                <a-input
                  @click.native="openOrganModel"
                  v-model="queryParam.accName"
                  readOnly
                  placeholder="请选择账套/核算单元"
                />
              </a-form-model-item>
            </a-col>
            <a-col :md="4" :sm="6">
              <a-form-model-item label="年度" prop="accYear">
                <a-date-picker
                  mode="year"
                  placeholder="请选择年度"
                  format="YYYY"
                  :allowClear="false"
                  value-format="YYYY"
                  v-model="queryParam.accYear"
                  :open="yearPickShow"
                  @panelChange="handlePanelChange"
                  @openChange="handleOpenChange"
                />
              </a-form-model-item>
            </a-col>
            <a-col :md="4" :sm="6">
              <a-form-model-item label="开始时间" prop="accStartTime">
                <a-date-picker
                  format="YYYY-MM-DD"
                  value-format="YYYY-MM-DD"
                  placeholder="请选择开始时间"
                  v-model="queryParam.accStartTime"
                />
              </a-form-model-item>
            </a-col>
            <a-col :md="4" :sm="6">
              <a-form-model-item label="结束时间" prop="accEndTime">
                <a-date-picker
                  format="YYYY-MM-DD"
                  value-format="YYYY-MM-DD"
                  placeholder="请选择结束时间"
                  v-model="queryParam.accEndTime"
                />
              </a-form-model-item>
            </a-col>
            <!-- <a-col :md="4" :sm="6">
              <a-form-model-item label="借贷关系">
            <a-radio-group v-model="queryParam.codeNumber" :options="options" :default-value="1" />-->
            <!-- <a-checkbox-group v-model="queryParam.codeNumber">
              <a-checkbox value="1">贷方</a-checkbox>
              <a-checkbox value="2" style="margin-left: 25px">借方</a-checkbox>
            </a-checkbox-group>-->
            <!-- </a-form-model-item>
            </a-col>-->
            <a-col :md="7" :sm="8">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">清除条件</a-button>
              <a-button
                type="danger"
                :loading="ydLoading"
                @click="handleToYD(false)"
                icon="arrow-right"
                style="margin-left: 8px"
                >转疑点</a-button
              >
              <a-button v-if="isBackButton" type="primary" style="margin-left: 8px" @click="backUppage">
                返回
              </a-button>
            </a-col>
          </a-row>
        </a-form-model>
      </div>
      <a-table
        ref="table"
        size="middle"
        bordered
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        @change="handleTableChange"
      >
        <template v-slot:unitName="text">
          <j-ellipsis :value="text" :length="20"></j-ellipsis>
        </template>
        <template v-slot:accName="text">
          <j-ellipsis :value="text" :length="15"></j-ellipsis>
        </template>
        <template v-slot:summary="text">
          <j-ellipsis :value="text" :length="10"></j-ellipsis>
        </template>
        <template v-slot:vchDate="text">
          <span>{{ text | TimerFormat }}</span>
        </template>
        <span slot="action" slot-scope="text, record">
          <a style="color: red" @click="handleToYD(record)">转疑点</a>
        </span>
      </a-table>
    </a-card>
    <CheckOrganModal ref="departModal" @ok="updateQueryParam"></CheckOrganModal>
    <y-d-model ref="YDModel" :routerData="$route.path"></y-d-model>
  </div>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { base64toImg } from '@/mixins/base64toImg'
import columns from './indexColumns'
import { httpAction, getAction, postAction, deleteAction } from '@/api/manage'
import moment from 'moment'
import { beforeyearYMD, beforeyearY, currentYMD } from '@/mixins/commontime.js'
import CheckOrganModal from '@/views/sj/sjxmzygl/yzzx/testcomponent/CheckOrganModal'
// import detailModel from './searchResult/index'
import YDModel from '@/views/sj/components/YDModel'
import jumpConfigMixin from '@/views/sj/sjxmzygl/sjxmss/jumpConfigMixin'
export default {
  name: 'ss',
  mixins: [JeecgListMixin, base64toImg, jumpConfigMixin],
  components: { CheckOrganModal, YDModel },
  data() {
    return {
      // 表头
      queryParam: {
        accName: '',
        accSetId: '',
        unitId: '',
        accYear: beforeyearY(2, 'years'),
        accStartTime: beforeyearYMD(2, 'years'),
        accEndTime: currentYMD(),
      },
      isBackButton: false,
      options: [
        { label: '贷方', value: -1 },
        { label: '借方', value: 1 },
      ],
      rules: {
        accYear: [{ required: true, message: '请选择年度' }],
        accStartTime: [{ required: true, message: '请选择开始时间' }],
        accEndTime: [{ required: true, message: '请选择结束时间' }],
        accName: [{ required: true, message: '请选择账套' }],
      },
      columns: columns(this),
      url: {
        list: '/sjkmfx/sjSubjectsAnalyse/querySubjectAnalyseInfo',
      },
      yearPickShow: false,
      dataSource: [],
      disableMixinCreated: true,
    }
  },
  computed: {},
  watch: {
    $route: {
      handler: function (to) {
        if (Object.keys(to.query).length) {
          this.disableMixinCreated = true
          this.queryParam = Object.assign(this.queryParam, to.query)
          this.$nextTick(() => {
            this.searchQuery()
          })
        } else if (Object.keys(to.params).length && to.params.from == 'sjxmss') {

          let { accName, accId } = to.params
          this.queryParam.accName = accName
          this.queryParam.accCode = accId

          let {endDate,startDate} = JSON.parse(to.params.returnRecord)
          this.queryParam.accYear = startDate&&startDate.split('-')[0]
          this.queryParam.accStartTime = startDate
          this.queryParam.accEndTime = endDate

          this.isBackButton = true
          this.returnRecord = to.params.returnRecord
          this.$nextTick(() => {
            this.loadData()
          })
        }
      },
      immediate: true,
    },
  },
  filters: {
    TimerFormat(time) {
      return time.indexOf(' ') ? time.split(' ')[0] : time
    },
  },
  methods: {
    searchReset() {
      this.queryParam = this.$options.data().queryParam
      this.dataSource = []
      this.ipagination = this.$options.data().ipagination
    },
    searchQuery() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.loadData(1)
        } else {
          return false
        }
      })
    },
    updateQueryParam(data) {
      console.log(data, 'daat')
      let str1 = ''
      let str2 = ''
      let str3 = ''
      data.map((item) => {
        // TODO 账套～～～
        str1 += item.accCode + ','
        str2 += item.accName + ','
        str3 += item.unitId + ','
      })
      if (str1.length > 0) {
        str1 = str1.substr(0, str1.length - 1)
      }
      if (str2.length > 0) {
        str2 = str2.substr(0, str2.length - 1)
      }
      if (str3.length > 0) {
        str3 = str3.substr(0, str3.length - 1)
      }
      this.queryParam.accSetId = str1
      this.queryParam.accName = str2
      this.queryParam.unitId = str3
    },
    openOrganModel() {
      this.$refs.departModal.edit()
    },
    handleAddSubmit(evt) {
      return new Promise((re, rj) => {
        postAction(this.url.add, evt).then((res) => {
          if (res.success) {
            this.$refs.table.loadData()
            this.$message.success(res.message)
            re(res.success)
          } else {
            rj(res.success)
            this.$message.error(res.message)
          }
        })
      })
    },
    handlePanelChange(value) {
      this.queryParam.accYear = moment(value).format('YYYY')
      this.yearPickShow = false
      this.$refs.form.validateField('accYear', (error) => {
        console.log(error)
      })
    },
    // 弹出日历和关闭日历的回调
    handleOpenChange(status) {
      this.yearPickShow = status
    },
    handleViewDetail(row) {
      this.$refs.detailModel.getPlanId(row)
      this.$refs.detailModel.visible = true
    },
    handleToYD(record) {
      if (this.dataSource.length) {
        const { accYear = '' } = this.queryParam
        let ydnr = undefined
        if (record) {
          ydnr = `【凭证编号：${record.accSetVchNo}】【科目编号：${record.subjectCode}】【科目名称：${record.subjectName}】`
        }
        let time = `【${accYear}】【科目对冲分析】`
        this.addYd(this.queryParam, ydnr, time)
      } else {
        this.$message.error('空数据不能新建疑点')
      }
    },
  },
}
</script>
<style lang="less" scoped>
@import '~@assets/less/common.less';
</style>